<?php

declare(strict_types=1);

namespace App\Model;


/**
 * @property int $id
 * @property int $type 1: 财富，2: 明星
 * @property int $level 等级，从1开始
 * @property string $level_name 等级称呼
 * @property int $integral 所需要的财富值或明星值
 */
class Level extends Model
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'tb_level';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = [];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'type' => 'integer', 'level' => 'integer', 'integral' => 'integer'];

    public function getLevel($integral = 0, $sex = 1)
    {

        $level_arr = $this->levelList($sex);
        $max_level = 1;
        foreach ($level_arr as $v) {
            $max_level = $v['level'];
            if ($integral >= $v['integral']) {
                $level = $v['level'];
                break;
            }
        }
        if (!isset($level)) {
            //顶级
            $level = $max_level;
        }
        return intval($level);
    }

    public function levelList($type)
    {

        // $cache = Mem::get('integral_level_'.$type);
        // if($cache === false){
        $cache = self::query()->select(['type', 'level', 'integral'])->where('type', $type)->orderBy('integral', 'desc')->get();
        // Mem::set('integral_level_'.$type, $cache, 300);
        // }

        return $cache;
    }
}
